﻿using SMALLERP;
using SMALLERP.PM;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace XJLSC.PM
{
    public partial class GRBGDItem : Form
    {
        DDKC g;
        DataTable dt;
        public event EventHandler DataSelected;
        public delegate void Cdgzzx(object sender, EventArgs e);
        //定义委托对象
        public Cdgzzx cdgzzx;

        public List<String> UserIds
        {
            get
            {
                return _UserIds;
            }
        }
        List<String> _UserIds = new List<string>();


        /// <summary>
        /// 用户名
        /// </summary>
        public List<String> UserNames
        {
            get
            {
                return _UserNames;
            }
        }
        List<String> _UserNames = new List<string>();

        public List<Double> OKQTYS
        {
            get
            {
                return _OKQTYS;
            }
        }
        List<Double> _OKQTYS = new List<Double>();

        public List<Double> NGQTYS
        {
            get
            {
                return _NGQTYS;
            }
        }
        List<Double> _NGQTYS = new List<Double>();

        public List<Double> MreQTYS
        {
            get
            {
                return _MreQTYS;
            }
        }
        List<Double> _MreQTYS = new List<Double>();

        public List<Double> PreQTYS
        {
            get
            {
                return _PreQTYS;
            }
        }
        List<Double> _PreQTYS = new List<Double>();

        public DataTable Returndt
        {
            get
            {
                return _Returndt;
            }
        }
        DataTable _Returndt;


        public GRBGDItem()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {

            this.dataGridView1.Controls.Clear();//移除所有控件 
            if (e.ColumnIndex.Equals(this.dataGridView1.Columns["ZYYG"].Index))
            //if (e.ColumnIndex == 13)
            //判断单元格是否是"Company"列？    
            {
                System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn    
                btn.Text = "...";//设置button文字    
                btn.Font = new System.Drawing.Font("Arial", 5);//设置文字格式    
                btn.Visible = true;//设置控件允许显示    

                btn.Width = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,
                                e.RowIndex, true).Height;//获取单元格高并设置为btn的宽    
                btn.Height = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,
                                e.RowIndex, true).Height;//获取单元格高并设置为btn的高    

                btn.Click += new EventHandler(btn_Click);//为btn添加单击事件    

                this.dataGridView1.Controls.Add(btn);//dataGridView1中添加控件btn    

                btn.Location = new System.Drawing.Point(((this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
                        (btn.Width)), this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);//设置btn显示位置    
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            if (IsExistWindow("DDKC"))
                return;
            g = new DDKC();
            g.kclx = "yuangongfx";
            g.DataSelected += new EventHandler(g_DataSelected);
            //f.cdgzzx += new SCKC.Cdgzzx(textBox2_TextChanged);
            g.MdiParent = this;
            g.Show(this);
        }
        private bool IsExistWindow(string name)
        {
            int iNum = this.MdiChildren.Length; //获得子窗体的数量
            for (int i = 0; i < iNum; i++) //循环检测是否存在名为name的窗体
            {
                if (MdiChildren[i].Name == name)
                {
                    MdiChildren[i].Focus();
                    return true;
                }
            }
            return false;
        }
        void g_DataSelected(object sender, EventArgs e)
        {
            String User = "";
            String Name = "";
            String ZXH = "";
            int rowindex = dataGridView1.CurrentRow.Index;
            for (int i = 0; i < g.UserIds.Count; i++)
            {
                User = g.UserIds[i].ToString();
                Name = g.UserNames[i].ToString();
                DataRow dr = ((DataTable)dataGridView1.DataSource).NewRow();
                dr["XH"] = BGMX.GY;
                ZXH = "000" + (rowindex + i + 1).ToString();
                ZXH = ZXH.Substring(ZXH.Length - 4, 4);
                dr["ZXH"] = ZXH;
                dr["ZYYG"] = User;
                dr["YGXM"] = Name;
                dr["OKQTY"] = BGMX.SL / g.UserIds.Count;
                dr["NGQTY"] = BGMX.BLSL / g.UserIds.Count;
                dr["MreQTY"] = BGMX.LFSL / g.UserIds.Count;
                dr["PreQTY"] = BGMX.GFSL / g.UserIds.Count;
                ((DataTable)dataGridView1.DataSource).Rows.Add(dr);
            }
            dataGridView1.Controls.Clear();
            dataGridView1.ClearSelection();
            dataGridView1.Rows[rowindex].Cells["ZYYG"].Selected = false;
            dataGridView1.CurrentCell = dataGridView1.Rows[rowindex].Cells["OKQTY"];
            dataGridView1.Rows[rowindex].Cells["OKQTY"].Selected = true;
            this.dataGridView1.BeginEdit(true);
        }

        private void GXBGDItem_Load(object sender, EventArgs e)
        {
            dt = GetDgvToTable(dataGridView1);

        }

        private void GXBGDItem_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DataSelected != null)
            {

                try
                {
                    this._UserIds.Clear();
                    this._UserNames.Clear();
                    this._OKQTYS.Clear();
                    this._NGQTYS.Clear();
                    this._MreQTYS.Clear();
                    this._PreQTYS.Clear();
                    this._Returndt = dt;
                    for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
                    {
                        if (dataGridView1.Rows[i].Cells["ZYYG"].Value.ToString() != "" && dataGridView1.Rows[i].Cells["XH"].Value.ToString() == BGMX.GY)
                        {
                            this._UserIds.Add(dataGridView1.Rows[i].Cells["ZYYG"].Value.ToString().Trim());
                            this._UserNames.Add(dataGridView1.Rows[i].Cells["YGXM"].Value.ToString().Trim());
                            this._OKQTYS.Add(Convert.ToDouble(dataGridView1.Rows[i].Cells["OKQTY"].Value));
                            this._NGQTYS.Add(Convert.ToDouble(dataGridView1.Rows[i].Cells["NGQTY"].Value));
                            this._MreQTYS.Add(Convert.ToDouble(dataGridView1.Rows[i].Cells["MreQTY"].Value));
                            this._PreQTYS.Add(Convert.ToDouble(dataGridView1.Rows[i].Cells["PreQTY"].Value));
                        }
                    }


                }
                catch
                {

                }
                // 引发事件
                this.DataSelected(null, null);

            }
            this.Hide();
            e.Cancel = true;




        }


        public DataTable GetDgvToTable(DataGridView dgv)
        {
            DataTable dt = new DataTable();

            try
            {
                // 列强制转换
                for (int count = 0; count < dgv.Columns.Count; count++)
                {
                    DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString());
                    dt.Columns.Add(dc);
                }

            }
            catch
            {
                return null;
            }

            return dt;
        }

        private void GXBGDItem_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible == true)
            {
                DataView dv = dt.DefaultView;
                dv.RowFilter = "XH='" + BGMX.GY + "'";
                dataGridView1.DataSource = dt;
            }
            else
            {

            }
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex.Equals(this.dataGridView1.Columns["OKQTY"].Index) || e.ColumnIndex.Equals(this.dataGridView1.Columns["NGQTY"].Index) || e.ColumnIndex.Equals(this.dataGridView1.Columns["MreQTY"].Index) || e.ColumnIndex.Equals(this.dataGridView1.Columns["PreQTY"].Index))
            {

                try
                {
                    double YBSL = 0.00;

                    for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
                    {
                        for (int j = 0; j <= dataGridView1.Columns.Count - 1; j++)
                        {

                            if (dataGridView1.Columns["OKQTY"].Index == j || dataGridView1.Columns["NGQTY"].Index == j || dataGridView1.Columns["MreQTY"].Index == j || dataGridView1.Columns["PreQTY"].Index == j)
                            {
                                double bcsl = (dataGridView1.Rows[i].Cells[j].Value == null) ? 0.00 : Convert.ToDouble(dataGridView1.Rows[i].Cells[j].Value);
                                YBSL += bcsl;
                            }
                        }
                    }

                    if (YBSL > BGMX.SL * 1.1)
                    {
                        MessageBox.Show("数量超报!最多还能报" + (BGMX.SL * 1.1 - YBSL + Convert.ToDouble(dataGridView1.CurrentCell.Value)), "软件提示");
                        this.dataGridView1.CurrentCell.Value = 0;
                    }
                }
                catch
                {
                    MessageBox.Show("出错了", "系统提示");
                }
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
        }

        private void GRBGDItem_Load(object sender, EventArgs e)
        {

        }
    }
}
